/*
 * Copyright (C) 2011 Nicola Bonzanni
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.vu.few.ibivu.jlemon;

import nl.vu.few.ibivu.jlemon.concepts.Edge;
import nl.vu.few.ibivu.jlemon.concepts.HasEdges;
import nl.vu.few.ibivu.jlemon.concepts.Node;
import nl.vu.few.ibivu.jlemon.listgraph.ListGraph;

public class IncidentEdgeIterator<E extends Edge & Comparable<E>> extends AbstractIterator<E> implements nl.vu.few.ibivu.jlemon.concepts.IncidentEdgeIterator<E> {

	private HasEdges<?,E,?> graph;
	private boolean direction;

	/**
	 * An iterator initialized with this constructor will behave as a
	 * standard java iterator, i.e. the state of the iterator before calling
	 * next() is undefined and the first call to next() will return the
	 * <b>first</b> edge incident to the given node (if any). If you desire
	 * an iterator that represent the first incident edge and such that the
	 * first call to next() returns the <b>second</b> incident edge, you
	 * should use IncidentEdgeIterator(graph.firstInc(this), node) instead.
	 * 
	 * @param node
	 *            a node belonging to the given graph
	 * @param graph TODO
	 * @see #IncidentEdgeIterator(Edge, Node)
	 * @see ListGraph#firstInc(IncidentEdgeIterator, Node)
	 */
	public <N extends Node & Comparable<N>> IncidentEdgeIterator(HasEdges<N,E,?> graph, N node) {
		super();
		this.graph = graph;
		next = graph.firstInc(this, node);
	}

	/**
	 * An iterator initialized with this constructor represent the given
	 * edge until the next() function is called. The first call to next()
	 * will return the edge next to the given one, and will set the iterator
	 * to represent the returned edge accordingly.
	 * 
	 * @param edge
	 *            an edge belonging to the given graph and incident to the
	 *            given node
	 * @param node
	 *            a node belonging to the given graph
	 * @param graph TODO
	 * @see #IncidentEdgeIterator(Node)
	 */
	public <N extends Node & Comparable<N>> IncidentEdgeIterator(HasEdges<N,E,?> graph, N node, E edge) {
		super(edge, null);
		this.graph = graph;
		setInherent(graph.u(edge).equals(node));
		next = graph.nextInc(this);
	}

	public void setInherent(boolean direction) {
		this.direction = direction;
	}

	public boolean isInherent() {
		return direction;
	}

	@Override
	protected E computeNext() {
		return graph.nextInc(this);
	}
}